using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class CarWindow : BaseWindow
{
	public static CarWindow instance;

	public GameObject anchorCenter;

	public GameObject AnchorRight;

	public GameObject interfaceForCarWithWeapon;

	public GameObject labelBullet;

	public GameObject sniperButton;

	public GameObject buttonShootL;

	public GameObject buttonShootR;

	public UITexture iconCarWeapon;

	public UISlider healthBar;

	public GameObject[] handBrakes;

	public UILabel lbCountCarBullets;

	[HideInInspector]
	public EntityBehavior vehicle;

	public GameObject TankAimDynamic;

	public GameObject TankAimStatic;

	public ReloadingUI reloadingUI;

	public GameObject tankSniperMode;

	public UILabel damageLabel;

	public TitanControlls titanControlls;

	public UILabel hitPercentObject;

	private Vector3 screenCenter = new Vector3((float)Screen.width / 2f, (float)Screen.height / 2f, 0f);

	public static int countCarBullets
	{
		set
		{
			if (instance != null && instance.lbCountCarBullets != null)
			{
				instance.lbCountCarBullets.text = ((GameController.instance.curTypeGame != TypeGame.tanks) ? value.ToString() : string.Empty);
			}
		}
	}

	private void Awake()
	{
		instance = this;
		hitPercentObject.gameObject.SetActive(false);
		tankSniperMode.SetActive(false);
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public void ShowHit(float damagePercent, Vector3 worldPosition)
	{
		UILabel hitPercent = Object.Instantiate(hitPercentObject);
		hitPercent.transform.SetParent(anchorCenter.transform);
		hitPercent.transform.localPosition = WorldToUI(worldPosition);
		hitPercent.transform.localScale = Vector3.one;
		hitPercent.text = "-" + damagePercent + "%";
		hitPercent.gameObject.SetActive(true);
		hitPercent.alpha = 0f;
		HOTween.To(hitPercent, 0.3f, new TweenParms().Prop("alpha", 1).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			HOTween.To(hitPercent, 1.3f, new TweenParms().Prop("alpha", 0).Ease(EaseType.EaseInOutExpo));
		}));
		Vector3 vector = hitPercent.transform.localPosition + Vector3.up * 60f;
		HOTween.To(hitPercent.transform, 1.6f, new TweenParms().Prop("localPosition", vector).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			Object.Destroy(hitPercent.gameObject);
		}));
	}

	private Vector3 WorldToUI(Vector3 position)
	{
		Vector3 result = PointerController.instance.mainCamera.WorldToScreenPoint(position);
		if (result.z < 0f)
		{
			result.x *= -1f;
			result.y *= -1f;
		}
		result -= screenCenter;
		result *= (float)PointerController.instance.uiRoot.activeHeight / (float)Screen.height;
		result.z = 0f;
		return result;
	}

	private void OnEnable()
	{
		reloadingUI.gameObject.SetActive(false);
		if (!settings.learningCompleted)
		{
			healthBar.gameObject.SetActive(false);
		}
		if ((bool)GameController.instance.playerScript && GameController.instance.playerScript.currentCar != null)
		{
			if (GameController.instance.playerScript.currentCar is TitanBehavior)
			{
				titanControlls.StartControlls(GameController.instance.playerScript.currentCar as TitanBehavior);
				titanControlls.gameObject.SetActive(true);
			}
			else
			{
				titanControlls.gameObject.SetActive(false);
			}
		}
	}

	private void OnDisable()
	{
	}

	private void Update()
	{
		if (vehicle != null)
		{
			float num = (float)vehicle.health / (float)vehicle.maxHealth;
			if (healthBar.value != num)
			{
				healthBar.value = num;
			}
		}
	}

	public override void OnPreShow(object userData)
	{
		anchorCenter.SetActive(true);
		AnchorRight.SetActive(true);
	}

	public override void OnHide()
	{
		anchorCenter.SetActive(false);
		AnchorRight.SetActive(false);
	}

	public void SetWeaponVisible(bool value)
	{
		interfaceForCarWithWeapon.SetActive(value);
		labelBullet.SetActive(value);
		sniperButton.SetActive(value);
		buttonShootL.SetActive(value);
		buttonShootR.SetActive(value);
	}
}
